class Solution {
public:
    int countNodes(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode *> q;
        q.push(root);
        int result = 0;
        while (!q.empty()) {
            int size = q.size();
            result += size;
            for (int i = 0; i < size; ++i) {
                TreeNode *node = q.front();
                q.pop();
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
        }
        return result;
    }

    int countNodes_1(TreeNode* root) {
        if (!root) return 0;
        int result = 1;
        if (root->left) result += countNodes(root->left);
        if (root->right) result += countNodes(root->right);
        return result;
    }
};
